<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <!-- 搜索添加区域 -->
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable>
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" class="btnLaet" @click="addGoods()">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表单列表 -->
      <el-table stripe border :data="goodsList" size="medium">
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格" prop="goods_price" width="80px"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="80px"></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="140px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteGoods(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      //   商品列表
      goodsList: [],
      //   分页页数
      queryInfo: {
        query: '',
        //   当前显示的页码
        pagenum: 1,
        // 显示的页数
        pagesize: 5
      },
      total: 0
    }
  },
  //   钩子函数
  created() {
    this.getGoodsList()
  },
  methods: {
    //   添加商品
    addGoods() {
      // 跳转到添加页面
      this.$router.push('/goods/add')
    },
    //   获取商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      this.total = res.data.total
      this.goodsList = res.data.goods
    },
    handleSizeChange(newsize) {
      // 监听显示页数的变化
      this.queryInfo.pagesize = newsize
      this.getGoodsList()
    },
    handleCurrentChange(newnum) {
      // 监听当前页数的变化
      this.queryInfo.pagenum = newnum
      this.getGoodsList()
    },
    // 删除商品
    async deleteGoods(uid) {
      const del = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (del !== 'confirm') return this.$message.error('已取消删除')
      //   否则就是点击了确定按钮，向后台发送请求
      const { data: res } = await this.$http.delete(`goods/ ${uid.goods_id}`)
      if (res.meta.status !== 200) return this.$message.error('删除失败')
      //   提醒用户删除成功并刷新页面
      this.$message.success('删除成功')
      this.getGoodsList()
    }
  }
}
</script>
<style lang="less" scoped>
.btnLaet {
  margin-left: 20px;
}
.el-table-column,
.el-table {
  font-size: 12px;
}
</style>
